package com.cuitrip.business.tripservice.ui;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;

import com.cuitrip.app.MainApplication;
import com.cuitrip.service.R;
import com.cuitrip.util.UIUtil;
import com.lab.adapter.BaseHolderView;
import com.lab.adapter.IAdapterData;
import com.lab.logtrack.AppLog;
import com.lab.utils.ToastUtil;


/**
 * 编辑文本
 *
 * @author ak
 */
public class EditTextHolderView extends BaseHolderView {

    private TextView mIconPoint;
    private EditText mEditText;

    public EditTextHolderView(Context context) {
        super(context, R.layout.layout_edit_text);
    }

    @Override
    public void initView(View rootView) {
        mIconPoint = UIUtil.findTextViewById(rootView, R.id.para_start_point);
        mEditText = UIUtil.findEditTextById(rootView, R.id.para_text);
        mIconPoint.setTypeface(MainApplication.getInstance().getIconFont());
        setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                mEditText.requestFocus();
            }
        });
        mEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
                if (i == EditorInfo.IME_ACTION_UNSPECIFIED) {
                    if (editTextCallBack != null) {
                        editTextCallBack.onKeyEnter(EditTextHolderView.this);
                        return true;
                    }
                }
                return false;
            }
        });
        mEditText.setOnKeyListener(new OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                switch (keyCode) {
                    case KeyEvent.KEYCODE_DEL:
                        if (mEditText.getText().length() == 0) {
                            editTextCallBack.onKeyClear(EditTextHolderView.this);
                        }
                        return false;
                }
                return false;
            }

        });
//        mEditText.addTextChangedListener(mEditTitleTextWatcher);
        mEditText.setTag(this);
    }

    @Override
    public void bindData(IAdapterData data, int position) {

    }

    private EditTextCallBack editTextCallBack;

    public void setEditTextCallBack(EditTextCallBack editTextCallBack) {
        this.editTextCallBack = editTextCallBack;
    }

    public void bindData(String data, int position) {
//        mEditText.removeTextChangedListener(mEditTitleTextWatcher);
        mEditText.setText(data);
        mEditText.addTextChangedListener(mEditTitleTextWatcher);
    }

    public String getText() {
        return mEditText.getText().toString();
    }

    public EditText getEditView(){
        return mEditText;
    }

    public interface EditTextCallBack {

        public void onKeyEnter(EditTextHolderView editTextHolderView);

        public void onKeyClear(EditTextHolderView editTextHolderView);

        /**
         * 当前文本输入变化
         * @return true 可以输入 false不能继续输入
         */
        public boolean onTextNumberChanged(int number);
    }

    private TextWatcher mEditTitleTextWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }

        @Override
        public void afterTextChanged(Editable editable) {
            try {
                int nSelStart = mEditText.getSelectionStart();
                int nSelEnd   = mEditText.getSelectionEnd();
                if (editTextCallBack != null){
                    boolean allowInput = editTextCallBack.onTextNumberChanged(editable.length());
                    if (!allowInput){
                        editable.delete(nSelStart - 1, nSelEnd);
                        mEditText.setTextKeepState(editable);
                    }
                }
            }catch (Exception e){
                AppLog.w(e.toString());
            }
        }
    };




    public int getEditSize(){
        return mEditText.getText().length();
    }
}
